﻿using MaterialDesignThemes.Wpf;
using Prism.Mvvm;

namespace Compass.Wpf.ViewModels.Dialogs;

public class DateAndLocationViewModel : BindableBase, IDialogHostAware
{
    public DateAndLocationViewModel()
    {
        SaveCommand = new DelegateCommand(Save);
        CancelCommand = new DelegateCommand(Cancel);
        Locations = Enum.GetNames(typeof(WarehouseLocation_e));
    }
    
    public string DialogHostName { get; set; }
    public DelegateCommand SaveCommand { get; set; }
    public DelegateCommand CancelCommand { get; set; }

    private string title;
    public string Title
    {
        get => title;
        set { title = value; RaisePropertyChanged(); }
    }
    private DateTime date;
    public DateTime Date
    {
        get => date;
        set { date = value; RaisePropertyChanged(); }
    }
    private WarehouseLocation_e location;
    public WarehouseLocation_e Location
    {
        get => location;
        set { location = value; RaisePropertyChanged(); }
    }
    private string[] locations;
    public string[] Locations
    {
        get => locations;
        set { locations = value; RaisePropertyChanged();}
    }


    private void Cancel()
    {
        if (DialogHost.IsDialogOpen(DialogHostName))
        {
            DialogHost.Close(DialogHostName, new DialogResult(ButtonResult.No));
        }
    }

    private void Save()
    {
        if (!DialogHost.IsDialogOpen(DialogHostName)) return;
        var param = new DialogParameters { { "Date", Date },{"Location",Location} };
        //保存时传递参数
        DialogHost.Close(DialogHostName, new DialogResult(ButtonResult.OK, param));
    }

    public void OnDialogOpen(IDialogParameters parameters)
    {
        if (parameters.ContainsKey("Title")) Title = parameters.GetValue<string>("Title");
        if (parameters.ContainsKey("Date")) Date = parameters.GetValue<DateTime>("Date");
        if (parameters.ContainsKey("Location")) Location = parameters.GetValue<WarehouseLocation_e>("Location");
    }
}